﻿using Microsoft.Office.Interop.Excel;
using System;
using System.IO;

namespace OfficeConvert.Core
{
    /// <summary>
    /// Excel转换类
    /// </summary>
    public class ExcelConvert : IOfficeConvert
    {
        public bool Convert(string sourceFullName, string targetFullName)
        {
            var extensionName = Path.GetExtension(targetFullName);

            switch (extensionName.ToUpper())
            {
                case ".HTML":
                    return ConvertFileFormat(sourceFullName, targetFullName, XlFileFormat.xlHtml);
                case ".CSV":
                    return ConvertFileFormat(sourceFullName, targetFullName, XlFileFormat.xlCSV);
                case ".PDF":
                    return ExportAsFixedFormat(sourceFullName, targetFullName, XlFixedFormatType.xlTypePDF);
                case ".XPS":
                    return ExportAsFixedFormat(sourceFullName, targetFullName, XlFixedFormatType.xlTypeXPS);
                default:
                    throw new Exception($"不支持的文件类型：{extensionName}");
            }
        }

        bool ConvertFileFormat(string sourceFullName, string targetFullName, XlFileFormat fileFormat)
        {
            ApplicationClass application = null;
            Workbook workbook = null;
            try
            {
                application = new ApplicationClass();
                workbook = application.Workbooks.Open(sourceFullName);
                // 进行另存为操作
                workbook.SaveAs(targetFullName, fileFormat, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return false;
            }
            finally
            {
                workbook?.Close();
                application?.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }

        bool ExportAsFixedFormat(string sourceFullName, string targetFullName, XlFixedFormatType xlFixedFormatType)
        {
            ApplicationClass application = null;
            Workbook workbook = null;
            try
            {
                application = new ApplicationClass();
                workbook = application.Workbooks.Open(sourceFullName);
                // 导出成指定格式
                workbook.ExportAsFixedFormat(xlFixedFormatType, targetFullName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                return false;
            }
            finally
            {
                workbook?.Close();
                application?.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }
    }
}
